package com.bustle.web.mbean.session;

import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;

import com.bustle.product.ProductDtoManager;
import com.bustle.product.ProviderDtoManager;
import com.bustle.product.data.Product.ProductType;
import com.bustle.product.dto.product.ProductDto;
import com.bustle.product.dto.provider.ProviderDto;

@ManagedBean(name = "product")
@SessionScoped
public class ProductMBean
{

  @EJB
  private ProductDtoManager productManager;

  @EJB
  private ProviderDtoManager providerManager;

  private ProductDto product;
  private ProviderDto provider;
  private List<ProductDto> listeAllProduct;
  private ProductType productTypeSelected;
  private String selected;
  private List<ProductType> productTypes;
  private List<ProviderDto> providers;
  private Integer idProviderSelected;
  private ProviderDto providerSearch;
  private int pAdd;
  private int pDetail;
  @ManagedProperty("#{cartMBean}")
  protected CartMBean cartMBean;


  @SuppressWarnings(
    {
      "static-access"
    })
  public String searchProduct()
  {
    String url = "";
    if (selected == "" && idProviderSelected == 000)
    {
      listeAllProduct = productManager.getListProduct();
    }
    else if (selected != "" && idProviderSelected != 000)
    {
      productTypeSelected = productTypeSelected.valueOf(selected);
      List<ProductDto> resultSearch = productManager.getSearchProductTypeProvider(productTypeSelected, idProviderSelected);
      listeAllProduct = resultSearch;
    }
    else if (selected != "" && idProviderSelected == 000)
    {
      productTypeSelected = productTypeSelected.valueOf(selected);
      List<ProductDto> resultSearch = productManager.getSearchProductProductType(productTypeSelected);
      listeAllProduct = resultSearch;
    }
    else if (selected == "" && idProviderSelected != 000)
    {
      List<ProductDto> resultSearch = productManager.getSearchProductProvider(idProviderSelected);
      listeAllProduct = resultSearch;
    }

    System.out.println("bla bal bla");
    System.out.println("affiche cat select " + selected);
    System.out.println("affiche id provider select " + idProviderSelected);

    return url;
  }

  public void addToCart(ActionEvent event)
  {
    System.out.println("produit numero : " + pAdd);
    pAdd = (int)event.getComponent().getAttributes().get("pAdd");
    System.out.println("produit numero : " + pAdd);
    addIntoCart(pAdd);
  }

  public void afficheDetails(ActionEvent event)
  {
    System.out.println(pDetail);
    pDetail = (int)event.getComponent().getAttributes().get("idProduit");
    System.out.println("id produit detail : " + pDetail);
    product = productManager.getById(pDetail);
    provider = product.getProvider();
  }

  public void setProductId(int aProductId)
  {
    System.out.println(pDetail);
    pDetail = aProductId;
    System.out.println("id produit detail : " + pDetail);
    product = productManager.getById(pDetail);
    provider = product.getProvider();
  }

  public void addToCartFromDetail(ActionEvent event)
  {
    addIntoCart(pDetail);
  }

  protected void addIntoCart(int aProductId)
  {
    CartMBean.LineitemBean cartLineitem = getCartLineitem(aProductId);
    if (cartLineitem == null)
    {
      cartLineitem = cartMBean.new LineitemBean();
      cartLineitem.setIdProduct(aProductId);
      cartLineitem.setQuantity(1);
      cartMBean.getLineitemBeans().add(cartLineitem);
    }
    else
    {
      cartLineitem.setQuantity(cartLineitem.getQuantity() + 1);
    }
  }

  protected CartMBean.LineitemBean getCartLineitem(int aProductId)
  {
    for (CartMBean.LineitemBean lineitem : cartMBean.getLineitemBeans())
    {
      if (lineitem.getIdProduct() == aProductId)
      {
        return lineitem;
      }
    }
    return null;
  }

  public void setCartMBean(CartMBean aCartMBean)
  {
    cartMBean = aCartMBean;
  }

  public List<ProductDto> getListeAllProduct()
  {
    if (selected == null)
      listeAllProduct = productManager.getListProduct();
    return listeAllProduct;
  }

  public void setListeAllProduct(List<ProductDto> listeAllProduct)
  {
    this.listeAllProduct = listeAllProduct;
  }

  public ProductDto getProduct()
  {
    return product;
  }

  public void setProduct(ProductDto product)
  {
    this.product = product;
  }

  public ProductType getProductTypeSelected()
  {
    return productTypeSelected;
  }

  public void setProductTypeSelected(ProductType productTypeSelected)
  {
    this.productTypeSelected = productTypeSelected;
  }

  public List<ProductType> getProductTypes()
  {
    productTypes = productManager.getProductType();
    return productTypes;
  }

  public void setProductTypes(List<ProductType> productTypes)
  {
    this.productTypes = productTypes;
  }

  public String getSelected()
  {
    return selected;
  }

  public void setSelected(String selected)
  {
    this.selected = selected;
  }

  public List<ProviderDto> getProviders()
  {
    providers = providerManager.getListProvider();
    return providers;
  }

  public void setProviders(List<ProviderDto> providers)
  {
    this.providers = providers;
  }

  public Integer getIdProviderSelected()
  {
    return idProviderSelected;
  }

  public void setIdProviderSelected(Integer idProviderSelected)
  {
    this.idProviderSelected = idProviderSelected;
  }

  public ProviderDto getProviderSearch()
  {
    return providerSearch;
  }

  public void setProviderSearch(ProviderDto providerSearch)
  {
    this.providerSearch = providerSearch;
  }

  public int getpAdd()
  {
    return pAdd;
  }

  public void setpAdd(int pAdd)
  {
    this.pAdd = pAdd;
  }

  public int getpDetail()
  {
    return pDetail;
  }

  public void setpDetail(int pDetail)
  {
    this.pDetail = pDetail;
  }

  public ProviderDto getProvider()
  {
    return provider;
  }

  public void setProvider(ProviderDto provider)
  {
    this.provider = provider;
  }

  //Constructeur du managed bean pour verif d'instanciation
  public ProductMBean()
  {
    System.out.println(this);
  }

}
